/* eslint-disable object-property-newline */
import React from 'react';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import {injectIntl, formatMessage} from './intl';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';
import {formatAmount, formatDateTime, formatPercent} from '../utils';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';

export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {pageIndex, pageSize, total, hasError, isDealerUser,
            data, loading, sortedColumnKey, sortedOrder} = this.props;
        const locale = {
            emptyText: hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.order',
                defaultMessage: '序号'
            }),
            dataIndex: 'order'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planCode',
                defaultMessage: '计划编号'
            }),
            dataIndex: 'planCode',
            sorter: true,
            sortOrder: sortedColumnKey === 'planCode' && sortedOrder,
            render: (text, record) => <Link to={routes.detail.format(record.dealerId)}>{text}</Link>
        }, {
            title: formatMessage({
                id: 'tablePanel.column.orderPlanYear',
                defaultMessage: '订单计划年份'
            }),
            dataIndex: 'orderPlanYear',
            sorter: true,
            sortOrder: sortedColumnKey === 'orderPlanYear' && sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.orderPlanMonth',
                defaultMessage: '订单计划月份'
            }),
            dataIndex: 'orderPlanMonth',
            sorter: true,
            sortOrder: sortedColumnKey === 'orderPlanMonth' && sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.createTime',
                defaultMessage: '计划提报日期'
            }),
            dataIndex: 'createTime',
            sorter: true,
            sortOrder: sortedColumnKey === 'createTime' && sortedOrder,
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalFee',
                defaultMessage: '计划金额'
            }),
            dataIndex: 'totalFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planVarietySum',
                defaultMessage: '计划品种'
            }),
            dataIndex: 'planVarietySum'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.emergencyAmount',
                defaultMessage: '紧急订单金额'
            }),
            dataIndex: 'emergencyAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.emergencyPercent',
                defaultMessage: '紧急订单占比'
            }),
            dataIndex: 'emergencyPercent',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.code',
                defaultMessage: '订单号'
            }),
            dataIndex: 'code',
            render: text => <Ellipsis tooltip fullWidthRecognition length={32}>{text}</Ellipsis>
        }, {
            title: formatMessage({
                id: 'tablePanel.column.submitTime',
                defaultMessage: '订单确认日期'
            }),
            dataIndex: 'submitTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.saleAmount',
                defaultMessage: '订单金额（常规订单）'
            }),
            dataIndex: 'saleAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.saleVarietySum',
                defaultMessage: '订单品种'
            }),
            dataIndex: 'saleVarietySum'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.conformVariety',
                defaultMessage: '符合品种'
            }),
            dataIndex: 'conformVariety'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planOrderVariety',
                defaultMessage: '计划和订单总品种'
            }),
            dataIndex: 'planOrderVariety'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planVarietyRate',
                defaultMessage: '计划品种符合率'
            }),
            dataIndex: 'planVarietyRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planMonthStockVarietyRate',
                defaultMessage: '计划当月备货品种满足率'
            }),
            dataIndex: 'planMonthStockVarietyRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planMonthStockAmountRate',
                defaultMessage: '计划当月备货金额满足率'
            }),
            dataIndex: 'planMonthStockAmountRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planMonthShipmentVarietyRate',
                defaultMessage: '计划当月发运品种满足率'
            }),
            dataIndex: 'planMonthShipmentVarietyRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.planMonthShipmentAmountRate',
                defaultMessage: '计划当月发运金额满足率'
            }),
            dataIndex: 'planMonthShipmentAmountRate',
            render: text => formatPercent(text)
        }];
        if(!isDealerUser)
            columns.splice(1, 0, ...[
                {
                    title: formatMessage({
                        id: 'tablePanel.column.dealerCode',
                        defaultMessage: '客户代码'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                    sortOrder: sortedColumnKey === 'dealerCode' && sortedOrder
                }, {
                    title: formatMessage({
                        id: 'tablePanel.column.country',
                        defaultMessage: '经销商'
                    }),
                    dataIndex: 'country',
                    sorter: true,
                    sortOrder: sortedColumnKey === 'country' && sortedOrder,
                },
            ]);
        return (
            <Card>
                <Table
                    className={'white-space-nowrap'}
                    columns={columns}
                    dataSource={data}
                    loading={loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={pagination}
                    rowKey="id"
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
